﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Collections;
namespace DFCommTest
{
    public partial class DFCTForm : Form
    {
        public DFCTForm()
        {
            InitializeComponent();
        }


        TcpListener m_tcpWait;
        Thread m_thdWait;

        TcpClient m_tcp;
        Encoding m_en = System.Text.Encoding.GetEncoding("big5");
        NetworkStream m_nsSend;
        private void button_Login_Click(object sender, EventArgs e)
        {
            if (m_tcp != null)
            {
                m_tcp.Close();
            }

            if (m_tcpWait == null)
            {
                m_tcpWait = new TcpListener(System.Convert.ToInt32(this.textBox_Port.Text));
                m_thdWait = new Thread(new ThreadStart(doWait));
                m_thdWait.Start();

            }

            m_tcp = new TcpClient();
            m_tcp.Connect(this.textBox_IP.Text, System.Convert.ToInt32(this.textBox_Port.Text));
            m_nsSend = m_tcp.GetStream();


            Send(this.textBox_ID.Text + ",A01," + this.textBox_ClientID.Text +",^");




        }

        TcpClient m_tcpAcc;
        public void doWait()
        {
            m_tcpWait.Start();
            while (true)
            {
                m_tcpAcc = m_tcpWait.AcceptTcpClient();
                
            }
        }

        

        public void Send(string arg_str)
        {
            try
            {
                if (m_nsSend != null)
                {

                    string strTemp = arg_str ;
                    Log("S:" + strTemp);
                    Log(hex2(strTemp));

                    byte[] bary = m_en.GetBytes(strTemp);

                    m_nsSend.Write(bary, 0, bary.Length);
                    m_nsSend.Flush();
                }
            }
            catch (Exception ex)
            {
                Log(ex.Message);
                Log(ex.StackTrace);
            }

        }



        public void Log(string arg)
        {
            string strTemp = System.DateTime.Now.ToString("HH:mm:ss") + "\t" + arg + "\r\n";
            this.textBox_Log.Text = strTemp + this.textBox_Log.Text;
            System.Diagnostics.Trace.WriteLine(strTemp);
        }


        public string hex2(string arg_str)
        {

            byte[] bary = m_en.GetBytes(arg_str);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bary.Length; i++)
            {
                sb.Append(bary[i].ToString("X"));
                sb.Append("|");
            }
            return sb.ToString();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void button_SendData_Click(object sender, EventArgs e)
        {
            //01(電腦代碼), I01, 05(話線), 電話, 姓名, 地址, 備註, 站別, 1, 目的地, 1(車數), 1(餘數), 0(記點), 0(特約金),預約日期 ,預約時間, 進線時間, | , 01(電腦代碼), 話務, ,,,,,X,YA^
            //{0}          ,I01,{1}      ,{2}  ,{3}  ,{4}  ,{5}  ,{6}  ,1 ,{7}    ,{8}     ,{9}     ,{10}    ,{11}      ,         ,        ,{12}     ,|  ,{0}          ,{13} ,,,,,,X,YA^


            string str = string.Format(this.textBox_Format.Text, textBox_ID.Text, comboBox_LineID.Text, comboBox_Tel.Text, comboBox_PnrName.Text
                , comboBox_Address.Text, comboBox_Memo.Text, comboBox_Station.Text, comboBox_DestAddr.Text, comboBox_CarQty.Text, comboBox_CarRemain.Text
                , comboBox_MarkPoint.Text, comboBox_TaxiFee.Text, textBox_InLineDT.Text, comboBox_Staff.Text, textBox_AppointD.Text, textBox_AppointT.Text);

            Send(str);


            
        }
    }
}